<?php

/**
* alltosun.com fighting_force.php index.php
* ============================================================================
* 版权所有 (C) 2009-2015 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 崔德龙 (cuidl@alltosun.com) $
* $Date: 15/6/17 上午11:11
* $Id$
*/

class Action
{
    private $user_id;

    public function __construct()
    {
        $this->user_id = '10000';
    }

    public function __call($action = '', $param = array()) {
        $id = intval($action);

        if (!$id) {
            return '未找到该记录';
        }

        $info = _uri('fighting_force', $id);

        if (!$info) {
            return '未找到该记录';
        }

        Response::assign('info', $info);
        Response::display('m/fighting_force_detail.html');
    }

    public function index()
    {
        Response::display('m/fighting_force.html');
    }

    // 下载图片
    public function download()
    {
        $server_id = Request::getParam('server_id', '');

        $access_token = js_api_helper::get_access_token();
        $url          = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$access_token.'&media_id='.$server_id;
        $data         = $this->httpGet($url);
        $return_info  = json_decode($data['body'], true);

        if ($return_info) {
            return '上传图片失败，请刷新页面重试~';
        }

        $res = $this->_upload($data['body']);

        if ($res == 'error' || !$res) {
            return '上传图片失败，请刷新页面重试~';
        }

        $force_key      = array_rand(events_config::$force_arr);
        $force_value    = events_config::$force_arr[$force_key];

        $data = array(
            'user_id'   => $this->user_id,
            'force'     => $force_key,
            'img'       => $res,
        );
        $id = _model('fighting_force')->create($data);

        return array('info'=>'ok', 'path'=>'/static/upload/'.$res, 'createinfo'=>$id, 'content'=>$force_value, 'force'=>$force_key);
    }

    // 上传文件
    private function _upload($file, $ext = 'jpg')
    {
        $time = time();
        $folder = UPLOAD_PATH.date('/Y/m/d', $time);

        if (!file_exists($folder) && !@mkdir($folder, 0777, true)) {
            return 'error';
        }

        static $count = 1;

        if (version_compare(PHP_VERSION, '5.2.2') >= 0) {
            $current_time = date('YmdHisu', $time);
        } else {
            $current_time = date('YmdHis', $time);
        }

        $random = mt_rand(0, 1000);
        $target = $folder.'/'.$current_time.'_'.$count.'_'.$random.'.'.$ext;

        $count++;

        $target    = trim($target, '.');
        $file_path = substr($target, strlen(UPLOAD_PATH));
        if (!file_exists($target)) {
            file_put_contents($target, $file);
        }

        return substr($target, strlen(UPLOAD_PATH));
    }

    private function httpGet($url) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_NOBODY, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($curl);
        $httpinfo = curl_getinfo($curl);
        curl_close($curl);
        $res = array_merge(array('header'=>$httpinfo),array('body'=>$package));
        return $res;
    }
}
?>